package cc.openkit.admin.controller.adv.box;

import cc.openkit.admin.model.AdvBox;
import cc.openkit.admin.model.AdvType;
import cc.openkit.admin.model.GGroupLimit;
import cc.openkit.admin.service.adv.box.AdvBoxService;
import cc.openkit.admin.service.g.GGroupLimitService;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import cc.openkit.kitIsNull.KitVerification;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告位管理
 *
 * @author bigben
 * @date 2018.4.2
 */
@Controller
@Scope("prototype")
@RequestMapping("/adv_box")
public class AdvBoxController {
    private Logger log = Logger.getLogger(AdvBoxController.class);

    @Resource
    private AdvBoxService advBoxService;

    @Resource
    private GGroupLimitService gGroupLimitService;

    /**
     * 去首次打开APP广告列表页面
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView index(HttpServletRequest request){
        log.info("跳转到推荐位管理页面");

        // 权限验证
        GGroupLimit gGroupLimit = gGroupLimitService.testGroup(request, 32);
        ModelAndView mv = new ModelAndView();
        // 没有权限，返回错误页面
        if(gGroupLimit==null){
            mv.setViewName("/admin/err/no_group_err");
            mv.addObject("msg", StaticFinalVar.NO_GROUP_MSG+StaticFinalVar.CALL_GROUP);
            return mv;
        }
        // 取值
        mv.addObject("kitG",gGroupLimit);
        mv.setViewName("/admin/adv_box/list");
        return mv;
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getAllJson", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllJson(HttpServletRequest request){
        log.info("获取推荐位的数据");

        Map<String,Object> map = new HashMap<String,Object>();

        String page = request.getParameter("page");// 获得页数
        String limit = request.getParameter("limit");// 获得每页显示条数
        String search = request.getParameter("search");// 获取搜索条件

        // 封装数据
        AdvBox advBox = new AdvBox();
        if(KitUtil.feikong(search)){
            advBox.setName(search);
        }

        // 分页查询
        List<AdvBox> list = advBoxService.queryPageListByWhere(advBox, Integer.valueOf(page), Integer.valueOf(limit));
        int size = advBoxService.queryCount(advBox);

        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",list);

        return JSONObject.toJSON(map);

    }

    /**
     * 去添加页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/goAdd", method = RequestMethod.GET)
    public Object goAdd(HttpServletRequest request){
        log.info("推荐位 》 添加 》 跳转");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/adv_box/add");
        return mv;
    }


    /**
     * 添加提交
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(HttpServletRequest request) throws Exception{
        log.info("广告模型添加 》 添加 》 保存");

        // 取值
        String name = request.getParameter("name");
        String explainFor = request.getParameter("explainFor");

        // 封装对象
        AdvBox advBox = new AdvBox();
        advBox.setName(name);
        Map<String, Object> map = KitVerification.verification(advBox);
        if(!"200".equals(map.get("code"))){
            return JSONObject.toJSON(map);
        }
        advBox.setExplainFor(explainFor);

        // 保存并返回
        return JSONObject.toJSON(advBoxService.save(advBox)==1 ? KitUtil.returnMap("200",StaticFinalVar.ADD_OK) : KitUtil.returnMap("101",StaticFinalVar.ADD_ERR));
    }

    /**
     * 删除
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Object del(HttpServletRequest request) throws Exception{
        log.info("删除广告状态");
        String id = request.getParameter("id");
        int i = advBoxService.deleteById(Integer.parseInt(id));
        return i==1?KitUtil.returnMap("200",StaticFinalVar.DEL_OK):KitUtil.returnMap("101",StaticFinalVar.DEL_ERR);
    }

    /**
     * 修改前的查询
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/goUpdate", method = RequestMethod.GET)
    public ModelAndView goUpdate(HttpServletRequest request) throws Exception{
        String id = request.getParameter("id");
        AdvBox model = advBoxService.queryById(Integer.parseInt(id));
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/adv_box/update");
        mv.addObject("kitModel",model);
        return mv;
    }

    /**
     * 修改
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws Exception{
        log.info("广告模型添加 》 修改 》 保存");

        // 取值
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String explainFor = request.getParameter("explainFor");

        // 封装对象
        AdvBox advBox = new AdvBox();
        advBox.setName(name);
        Map<String, Object> map = KitVerification.verification(advBox);
        if(!"200".equals(map.get("code"))){
            return JSONObject.toJSON(map);
        }
        advBox.setId(Integer.valueOf(id));
        advBox.setExplainFor(explainFor);

        // 修改保存并返回参数
        return JSONObject.toJSON(advBoxService.updateSelective(advBox)==1 ? KitUtil.returnMap("200",StaticFinalVar.UPDATE_OK) : KitUtil.returnMap("101",StaticFinalVar.UPDATE_ERR));
    }

}
